//
//  ZSHomeViewController.m
//  ZSMiao
//
//  Created by zsj1992 on 16/12/1.
//  Copyright © 2016年 bjhj. All rights reserved.
//

#import "ZSHomeViewController.h"
#import "ZSHotViewController.h"
#import "ZSNewViewController.h"
#import "ZSLikeViewController.h"

@interface ZSHomeViewController ()<UIScrollViewDelegate>

@property (nonatomic,strong)UIButton * btnSelected;
@property (nonatomic,strong)NSMutableArray * pageBtns;
@property (nonatomic,strong)UIScrollView * contentView;

@property (nonatomic,strong)ZSHotViewController * hotVc;

@end

@implementation ZSHomeViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navigationItem.title = @"";
   
    //让bar透明
    self.navigationController.navigationBar.translucent = YES;
    
    [self.navigationController.navigationBar setTintColor:RGBA(0, 134, 225,1)];

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBar_bg_414x70zs"] forBarMetrics:UIBarMetricsDefault];
    
    [self setupPageView];
    
    [self setupContentView];
    
    [self setupSubViews];
}

static UIView * indicatorView;
static UIView * navContentView;



//-(void)loadView{
//    self.contentView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H-44)];
//    self.contentView.delegate = self;
//    self.contentView.showsHorizontalScrollIndicator = NO;
//    [self.view addSubview:self.contentView];
//
//}


-(void)setupPageView{

    navContentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, 44)];
    [self.navigationController.navigationBar addSubview:navContentView];

    //最新
    UIButton * btnNew = [[UIButton alloc]init];
    [btnNew setTitle:@"最新" forState:(UIControlStateNormal)];
    btnNew.titleLabel.font = [UIFont systemFontOfSize:15];
    [btnNew setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateNormal)];
    [btnNew setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
    btnNew.titleLabel.textAlignment = NSTextAlignmentCenter;
    [navContentView addSubview:btnNew];
    [btnNew mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(navContentView);
        make.width.mas_equalTo(40);
    }];
    [btnNew addTarget:self action:@selector(btnPageClick:) forControlEvents:(UIControlEventTouchUpInside)];
    btnNew.tag = 1001;
    
    //最热
    UIButton * btnHot = [[UIButton alloc]init];
    [btnHot setTitle:@"最热" forState:(UIControlStateNormal)];
    btnHot.titleLabel.font = [UIFont systemFontOfSize:15];
    [btnHot setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateNormal)];
    [btnHot setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
    btnHot.titleLabel.textAlignment = NSTextAlignmentCenter;
    [navContentView addSubview:btnHot];
    [btnHot mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(navContentView);
        make.right.equalTo(btnNew.mas_left).offset(-20);
        make.width.mas_equalTo(40);
    }];
    [btnHot addTarget:self action:@selector(btnPageClick:) forControlEvents:(UIControlEventTouchUpInside)];
    btnHot.tag = 1000;
    
    //关注
    UIButton * btnLike = [[UIButton alloc]init];
    [btnLike setTitle:@"关注" forState:(UIControlStateNormal)];
    btnLike.titleLabel.font = [UIFont systemFontOfSize:15];
    [btnLike setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateNormal)];
    [btnLike setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
    btnLike.titleLabel.textAlignment = NSTextAlignmentCenter;
    [navContentView addSubview:btnLike];
    [btnLike mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(navContentView);
        make.left.equalTo(btnNew.mas_right).offset(20);
        make.width.mas_equalTo(40);
    }];
    [btnLike addTarget:self action:@selector(btnPageClick:) forControlEvents:(UIControlEventTouchUpInside)];
    btnLike.tag = 1002;
    

    //指示条
    indicatorView = [[UIView alloc]init];
    indicatorView.backgroundColor = [UIColor whiteColor];
    [navContentView addSubview:indicatorView];
    [indicatorView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.bottom.equalTo(navContentView.mas_bottom);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(2);
    }];
    
#pragma mark-zsj

    [navContentView layoutIfNeeded];
    MyLog(@"%f=====%f=====%f",btnHot.frame.origin.x,btnNew.frame.origin.x,btnLike.frame.origin.x);

    
    self.pageBtns = [NSMutableArray array];
    [self.pageBtns addObjectsFromArray:@[btnHot,btnNew,btnLike]];
    
    
    [self btnPageClick:self.pageBtns[0]];

    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    navContentView.hidden = NO;
    

}


//点击上面的翻页按钮
-(void)btnPageClick:(UIButton *)sender{
    
    for (UIButton * btn in self.pageBtns) {
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
    }
    
    
    self.btnSelected.selected = NO;
    self.btnSelected = sender;
    self.btnSelected.selected = YES;
    self.btnSelected.titleLabel.font = [UIFont systemFontOfSize:17];

#pragma mark-zsj

    CGFloat X = sender.frame.origin.x-10;
    
    [indicatorView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(X);

    }];
    
    [UIView animateWithDuration:0.25 animations:^{
        [navContentView layoutIfNeeded];
    }];
    
    if (sender.tag == 1000) {
        [self.contentView setContentOffset:CGPointMake(0, -64) animated:YES];
    }else if (sender.tag==1001){
        [self.contentView setContentOffset:CGPointMake(SCREEN_W, -64) animated:YES];
    }else if (sender.tag==1002){
        [self.contentView setContentOffset:CGPointMake(SCREEN_W*2, -64) animated:YES];
    }
    
    
    MyLog(@"sss");
}



-(void)setupContentView{

    self.contentView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H-44)];
    self.contentView.delegate = self;
    self.contentView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.contentView];
    
    
    
}

-(void)setupSubViews{

    ZSHotViewController * hotVc = [[ZSHotViewController alloc]init];
    [self addChildViewController:hotVc];
    hotVc.view.frame = self.contentView.bounds;
    [self.contentView addSubview:hotVc.view];
    self.hotVc = hotVc;
    
    
    ZSNewViewController * newVc = [[ZSNewViewController alloc]init];
    [self addChildViewController:newVc];
    newVc.view.frame = CGRectMake(SCREEN_W, 0, SCREEN_W, SCREEN_H-104);
    [self.contentView addSubview:newVc.view];
    
    ZSLikeViewController * likeVc = [[ZSLikeViewController alloc]init];
    [self addChildViewController:likeVc];
    likeVc.view.frame = CGRectMake(SCREEN_W*2, 0, SCREEN_W, SCREEN_H-104);
    [self.contentView addSubview:likeVc.view];
    
    self.contentView.contentSize = CGSizeMake(3*SCREEN_W, 0);
    self.contentView.pagingEnabled = YES;

}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    CGFloat offset = scrollView.contentOffset.x;
    NSInteger page = offset/SCREEN_W;
    
    if (scrollView==self.contentView) {
        
        [self btnPageClick:self.pageBtns[page]];

    }
    
    
    
}






-(void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];
    
    navContentView.hidden = YES;


}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
